package com.visikard.ui.fragment;

/**
 * effect fragment use on kard builder
 */
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.visikard.item.GetLocationSpecialItem;
import com.visikard.item.ItemCouponsBuilderCreate;
import com.visikard.ui.R;
import com.visikard.ui.activity.CouponBuilderActivity;
import com.visikard.utility.Utils;
import com.visikard.widget.DatePickerFragment;

public class CBDetailFragment extends BaseFragment implements OnClickListener, SetTimeParent{

	EditText business_name,  business_address, coupon_name, percent_off, write_up;
	TextView start_date_month, start_date_day, start_date_year,
			 end_date_month, end_date_day, end_date_year, participating_locations;
	Button btn_next;
	static long TIME_MAX = 0, 	TIME_MIN = 0;
	
	//child fragment
	CBInputLocationFragment cbInputLocationFragment;
	Date startDate, endDate;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.cb_detail, container, false);
		business_name = (EditText) v.findViewById(R.id.category_name);
		business_address = (EditText) v.findViewById(R.id.sub_category_address);
		coupon_name = (EditText) v.findViewById(R.id.coupon_name);
		percent_off = (EditText) v.findViewById(R.id.percent_off);
		write_up = (EditText) v.findViewById(R.id.write_up);
		participating_locations = (TextView) v.findViewById(R.id.participating_locations);
		
		start_date_month = (TextView) v.findViewById(R.id.start_date_month);
		start_date_day = (TextView) v.findViewById(R.id.start_date_day);
		start_date_year = (TextView) v.findViewById(R.id.start_date_year);
		end_date_month = (TextView) v.findViewById(R.id.end_date_month);
		end_date_day = (TextView) v.findViewById(R.id.end_date_day);
		end_date_year = (TextView) v.findViewById(R.id.end_date_year);
		btn_next = (Button) v.findViewById(R.id.btn_next);
		
		v.findViewById(R.id.home_btn_menu).setOnClickListener(this);
		start_date_day.setOnClickListener(this);
		start_date_month.setOnClickListener(this);
		start_date_year.setOnClickListener(this);
		end_date_month.setOnClickListener(this);
		end_date_day.setOnClickListener(this);
		end_date_year.setOnClickListener(this);
		btn_next.setOnClickListener(this);
		participating_locations.setOnClickListener(this);
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		TIME_MAX = Calendar.getInstance().getTimeInMillis();
		TIME_MIN = TIME_MAX;
		
		//Edit coupon
		if(CouponBuilderActivity.itemCouponBuilderCreate != null){
			setData();
		}

	}
	
	private void setData(){
		business_name.setText(CouponBuilderActivity.itemCouponBuilderCreate.getCategoryName());
		business_address.setText(CouponBuilderActivity.itemCouponBuilderCreate.getSubCategoryName());
		coupon_name.setText(CouponBuilderActivity.itemCouponBuilderCreate.getCouponName());
		percent_off.setText(""+CouponBuilderActivity.itemCouponBuilderCreate.getCampaignDetailValue());
		write_up.setText(CouponBuilderActivity.itemCouponBuilderCreate.getAboutbusiness());
		List<GetLocationSpecialItem> locations = CouponBuilderActivity.itemCouponBuilderCreate.getLocations();
		if(locations != null && locations.size() > 0){
			participating_locations.setText(locations.get(0).getAddress());
		}
		String[] date_start = CouponBuilderActivity.itemCouponBuilderCreate.getStartDate().split("/");
		if(date_start.length >2){
			start_date_day.setText(String.format("%02d", Integer.parseInt(date_start[1])));
			start_date_month.setText(String.format("%02d", Integer.parseInt(date_start[0])));
			start_date_year.setText(String.format("%04d", Integer.parseInt(date_start[2])));
			Calendar cal =Calendar.getInstance();
			cal.set(Calendar.YEAR, Integer.parseInt(date_start[2]));
			cal.set(Calendar.MONTH, Integer.parseInt(date_start[0]));
			cal.set(Calendar.DATE, Integer.parseInt(date_start[1]));
			startDate = cal.getTime();
		}
		
		String[] end_date = CouponBuilderActivity.itemCouponBuilderCreate.getEndDate().split("/");
		if(end_date.length >2){
			end_date_day.setText(String.format("%02d", Integer.parseInt(end_date[1])));
			end_date_month.setText(String.format("%02d", Integer.parseInt(end_date[0])));
			end_date_year.setText(String.format("%04d", Integer.parseInt(end_date[2])));
			Calendar cal =Calendar.getInstance();
			cal.set(Calendar.YEAR, Integer.parseInt(end_date[2]));
			cal.set(Calendar.MONTH, Integer.parseInt(end_date[0]));
			cal.set(Calendar.DATE, Integer.parseInt(end_date[1]));
			endDate = cal.getTime();
		}
		
	}
	
	public void showBackDatePickerDialog() {
		DatePickerFragment newFragment = new DatePickerFragment();
		newFragment.setNotSetMaxBackDate(true);
		newFragment.setParent(this, TIME_MIN, TIME_MAX, false);
		newFragment.show(getActivity().getSupportFragmentManager(),
				"datePicker");
	}

	public void showNextDatePickerDialog() {
		DatePickerFragment newFragment = new DatePickerFragment();
		newFragment.setNotSetMaxBackDate(true);
		newFragment.setParent(this, TIME_MIN, TIME_MAX, true);
		newFragment.show(getActivity().getSupportFragmentManager(),
				"datePicker");
	}

	@Override
	public String getBackdayStr() {
		String date = "";
		date = start_date_year.getText() + "-" + start_date_month.getText() + "-" + start_date_day.getText();
		if(date.contains("MM")){
			date = Utils.format_MM_DD_YYYY(System.currentTimeMillis());
		}
		return date;
	}

	@Override
	public void setBackDate(int year, int month, int day) {
		start_date_day.setText(String.format("%02d", day));
		start_date_month.setText(String.format("%02d", (month+1)));
		start_date_year.setText(String.format("%04d", year));
		Calendar cal =Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.DATE, day);
		startDate = cal.getTime();
	}

	@Override
	public String getNextdayStr() {
		String date = "";
		date = end_date_year.getText() + "-" +  end_date_month.getText() + "-" + end_date_day.getText();
		if(date.contains("MM")){
			date = Utils.format_MM_DD_YYYY(System.currentTimeMillis());
		}
		return date;
	}

	@Override
	public void setNextDate(int year, int month, int day) {
		// TODO Auto-generated method stub
		end_date_day.setText(String.format("%02d", day));
		end_date_month.setText(String.format("%02d", (month+1)));
		end_date_year.setText(String.format("%04d", year));
		Calendar cal =Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.DATE, day);
		endDate = cal.getTime();
	}

	@Override
	public void setTimeMin(long timeMin, long timeMax) {
		// TODO Auto-generated method stub
		TIME_MIN = timeMin;
		TIME_MAX = timeMax;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.start_date_day:
		case R.id.start_date_month:
		case R.id.start_date_year:
			showBackDatePickerDialog();
			break;
		case R.id.end_date_day:
		case R.id.end_date_month:
		case R.id.end_date_year:
			showNextDatePickerDialog();
			break;
		case R.id.btn_next:
			if(validateData()){
				saveCouponForCreate();
				((CouponBuilderActivity)getActivity()).nextFragment();
			}
			break;
		case R.id.participating_locations:
			shoLocationInput();
			break;
		case R.id.home_btn_menu:
			((CouponBuilderActivity)getActivity()).onBackPressed();
			break;
		default:
			break;
		}
	}
	
	private void shoLocationInput(){
		FragmentManager fm = getChildFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		cbInputLocationFragment = (CBInputLocationFragment) fm.findFragmentByTag(CBInputLocationFragment.TAG);
		if(cbInputLocationFragment == null){
			cbInputLocationFragment = new CBInputLocationFragment();
			 ft.add(R.id.child_fragment_container, cbInputLocationFragment, CBInputLocationFragment.TAG);
			 ft.show(cbInputLocationFragment);
			 ft.commit();
		}
		else{
			ft.show(cbInputLocationFragment);
			ft.commit();
		}
	}
	
	public void setLocationResultAndHideChildFragment(String location){
		FragmentManager fm = getChildFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		cbInputLocationFragment = (CBInputLocationFragment) fm.findFragmentByTag(CBInputLocationFragment.TAG);
		if(cbInputLocationFragment != null){
			ft.remove(cbInputLocationFragment);
			ft.commit();
		}
		if(location != null)
			participating_locations.setText(location);
	}
	
	private boolean validateData(){
		boolean result = false;
		if(coupon_name.getText().toString().trim().equals("")){
			Toast.makeText(getActivity().getApplicationContext(),getString(R.string.alert_empty, coupon_name.getHint().toString()),
					Toast.LENGTH_SHORT).show();
		}
//		else if (participating_locations.getText().toString().equals("")){
//			Toast.makeText(getActivity().getApplicationContext(),getString(R.string.alert_empty, participating_locations.getHint().toString()),
//					Toast.LENGTH_SHORT).show();
//		}
		else if (start_date_month.getText().toString().equals("MM")){
			Toast.makeText(getActivity().getApplicationContext(),getString(R.string.alert_empty, "Start Date"),
					Toast.LENGTH_SHORT).show();
		}
		else if (end_date_month.getText().toString().equals("MM")){
			Toast.makeText(getActivity().getApplicationContext(),getString(R.string.alert_empty, "End Date"),
					Toast.LENGTH_SHORT).show();
		}
		
		else{
			if(endDate.compareTo(startDate) < 0){
				Toast.makeText(getActivity().getApplicationContext(),getString(R.string.alert_enddate_startdate),
						Toast.LENGTH_SHORT).show();
			}
			else{
				result = true;
			}
		}
		
		return result;
	}
	
	private void saveCouponForCreate(){
		
		if(CouponBuilderActivity.itemCouponBuilderCreate == null){
			CouponBuilderActivity.itemCouponBuilderCreate = new ItemCouponsBuilderCreate();
		}
		
		CouponBuilderActivity.itemCouponBuilderCreate.setCouponName(coupon_name.getText().toString().trim());
		
		try{
			CouponBuilderActivity.itemCouponBuilderCreate.setCampaignDetailValue(Integer.parseInt(percent_off.getText().toString().trim()));
		}
		catch (Exception e){
			
		}
		
		CouponBuilderActivity.itemCouponBuilderCreate.setImageText1(CouponBuilderActivity.itemCouponBuilderCreate.getCampaignDetailValue() +"%" + " OFF");
		CouponBuilderActivity.itemCouponBuilderCreate.setStartDate(start_date_month.getText() + "/" + start_date_day.getText() + "/" + start_date_year.getText());
		CouponBuilderActivity.itemCouponBuilderCreate.setsEndDate(end_date_month.getText() + "/" + end_date_day.getText() + "/" + end_date_year.getText());
		CouponBuilderActivity.itemCouponBuilderCreate.setAboutbusiness(write_up.getText().toString().trim());
		CouponBuilderActivity.itemCouponBuilderCreate.setExpireDate(CouponBuilderActivity.itemCouponBuilderCreate.getEndDate());
		
	}
	
}
